﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

namespace UnityEngine.UI
{
    public class UICanvasRebuilder : MonoBehaviour
    {
        public List<UIBehaviour> m_layouts = new List<UIBehaviour>();
        public List<UIBehaviour> m_graphics = new List<UIBehaviour>();
        public void OnEnable()
        {
            foreach (var layout in m_layouts)
            {
                layout.enabled = true;
            }
    
            foreach (var graphic in m_graphics)
            {
                graphic.enabled = true;
            }
        }
    
        public void OnDisable()
        {
            foreach (var layout in m_layouts)
            {
                layout.enabled = false;
            }
    
            foreach (var graphic in m_graphics)
            {
                graphic.enabled = false;
            }
        }
        
        
        
    #if UNITY_EDITOR
        private void OnValidate()
        {
            if (Application.isPlaying)
            {
                return;
            }
            
            UIBehaviour element;
            
            var layouts = new List<ILayoutController>();
            GetComponentsInChildren<ILayoutController>(true, layouts);
            m_layouts.Clear();
            foreach (var layout in layouts)
            {
                element = (UIBehaviour) layout;
                m_layouts.Add(element);
            }
            
            var graphics = new List<ICanvasElement>();
            GetComponentsInChildren<ICanvasElement>(true, graphics);
            m_graphics.Clear();
            foreach (var graphic in graphics)
            {
                element = (UIBehaviour) graphic;
                m_graphics.Add(element);
            }

            if (enabled)
            {
                OnEnable();
            }
            else
            {
                OnDisable();
            }
            
        }
        
        /*
        static UICanvasRebuilder()
        {
            UnityEditor.Experimental.SceneManagement.PrefabStage.prefabStageOpened += PrefabStageOpened;
            UnityEditor.Experimental.SceneManagement.PrefabStage.prefabStageClosing += PrefabStageClosing;
        }

        private static void PrefabStageOpened(UnityEditor.Experimental.SceneManagement.PrefabStage stage)
        {
            var rebuilder = stage.prefabContentsRoot.GetComponent<UICanvasRebuilder>();
            if (rebuilder != null)
            {
                stage.prefabContentsRoot.SetActive(true);
                if (rebuilder.enabled == false)
                {
                    rebuilder.enabled = true;
                    rebuilder.OnEnable();
                }
            }
        }
        private static void PrefabStageClosing(UnityEditor.Experimental.SceneManagement.PrefabStage stage)
        {
            var rebuilder = stage.prefabContentsRoot.GetComponent<UICanvasRebuilder>();
            if (rebuilder != null)
            {
                var prefab = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(stage.prefabAssetPath);
                rebuilder = prefab.GetComponent<UICanvasRebuilder>();
                if (rebuilder.enabled)
                {
                    rebuilder.enabled = false;
                    rebuilder.OnDisable();
                }
                
                prefab.SetActive(false);
                UnityEditor.EditorUtility.SetDirty(prefab);
            }
            
        }*/
#endif
    }
}
